﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using YunPian.Tools;

namespace YunPian
{
  public class Client
    {

        private string _apiKey = "";
        private string _apiDomain = "https://sms.yunpian.com/";
        private string _usApiDomain = "https://us.yunpian.com/";
        private string _version = "v2";

        public Client(string ApiKey)
        {
            _apiKey = ApiKey;
        }

        public Client(string ApiKey,string Version)
        {
            _apiKey = ApiKey;
            _version = Version;
        }


        public T Execute<T>(IBaseRequest<T> request,bool isForeign=false) where T: BaseResponse
        {

            var url = $"{(isForeign?_usApiDomain:_apiDomain)}{_version}/{request.GetUrl()}";

            if (request.GetMetohd() == "POST") 
            {
                var body = request.GetBody();

                if (body.Length > 0)
                {
                    body = "apikey="+_apiKey + "&" + body;
                }
                else {

                    body = _apiKey;
                }


                var r = WebHelper.HttpPost(url, body);

                return JsonConvert.DeserializeObject<T>(r);

            }
            return default(T);
                 
        }




    }
}
